package com.sc.study.exam.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.sc.study.exam.domain.*;
import com.sc.study.exam.mapper.ScStudentTestexamMapper;
import com.sc.study.exam.mapper.ScTestMapper;
import com.sc.study.exam.mapper.ScTestexamQuestionMapper;
import com.sc.study.exam.mapper.ScTestexamTypeMapper;
import com.sc.study.exam.service.IScStudentTestexamService;
import com.sc.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sc.study.exam.mapper.ScTestexamMapper;
import com.sc.study.exam.service.IScTestexamService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 考试Service业务层处理
 *
 * @author sc
 * @date 2022-02-27
 */
@Service
public class ScTestexamServiceImpl implements IScTestexamService {
    @Autowired
    private ScTestexamMapper scTestexamMapper;

    @Autowired
    private ScTestexamQuestionMapper scTestexamQuestionMapper;

    @Autowired
    private ScStudentTestexamMapper scStudentTestexamMapper;

    @Autowired
    private ScTestexamTypeMapper scTestexamTypeMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private ScTestMapper scTestMapper;

    @Autowired
    private IScStudentTestexamService scStudentTestexamService;

    /**
     * 根据试卷id查询
     * @param testId 试卷id
     * @return 返回根据testId查询的id， 若没有则为空
     */
    @Override
    public Integer selectByTestId(Integer testId) {
        return scTestexamMapper.selectByTestId(testId);
    }

    /**
     * 查询考试
     *
     * @param id 考试主键
     * @return 考试
     */
    @Override
    public ScTestexam selectScTestexamById(Long id) {
        return scTestexamMapper.selectScTestexamById(id);
    }

    /**
     * 查询考试列表
     *
     * @param scTestexam 考试
     * @return 考试
     */
    @Override
    public List<ScTestexam> selectScTestexamList(ScTestexam scTestexam) {
        List<ScTestexam> scTestexams = scTestexamMapper.selectScTestexamList(scTestexam);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        //获取当前时间
        Date currentTime = new Date();


        //设置创建人 和 老师
        scTestexams.forEach(item -> {

            //设置是否显示删除按钮
            try {
                Date startTime = simpleDateFormat.parse(item.getStarttime());
                Date endTime = simpleDateFormat.parse(item.getEndtime());

                //考试途中不可修改 或者已经开考了不可更改学生
                if(currentTime.after(startTime) && currentTime.before(endTime)) item.setShowUpdateStudentButton(false);
                if(currentTime.after(endTime))  item.setShowUpdateStudentButton(false);

            } catch (ParseException e) {
                e.printStackTrace();
            }

            item.setScTestexamType(scTestexamTypeMapper.selectScTestexamTypeById(item.getTestexamtypeid()));
            item.setSysUser(sysUserMapper.selectUserById(item.getUserid()));
        });
        return scTestexams;
    }

    /**
     * 新增考试
     *
     * @param scTestexam 考试
     * @return 结果
     */
    @Override
    @Transactional
    public int insertScTestexam(ScTestexam scTestexam) {
        long userId = scTestexam.getUserid();
        ScTest scTest = new ScTest(scTestexam.getName(),userId);
        scTestMapper.insertScTest(scTest);
        scTestexam.setTestid(scTest.getId());
        int count = scTestexamMapper.insertScTestexam(scTestexam);
        List<ScQuestion> scQuestions = JSON.parseArray(scTestexam.getScQuestion(), ScQuestion.class);
        List<Integer> student = JSON.parseArray(scTestexam.getStudents(), Integer.class);
        scQuestions.stream().forEach(item -> {
            ScTestexamQuestion scTestexamQuestion = new ScTestexamQuestion(scTest.getId(), item.getId(), item.getScore());
            scTestexamQuestionMapper.insertScTestexamQuestion(scTestexamQuestion);
        });
        student.forEach(item -> {
            ScStudentTestexam scStudentTestexam = new ScStudentTestexam();
            scStudentTestexam.setTestexamid(scTestexam.getId());
            scStudentTestexam.setStarttime(scTestexam.getStarttime());
            scStudentTestexam.setEndtime(scTestexam.getEndtime());
            scStudentTestexam.setStudentid(item.longValue());
            scStudentTestexam.setState(1L);
            scStudentTestexamMapper.insertScStudentTestexam(scStudentTestexam);
        });
        return count;
    }


    @Override
    public int addScTestexam(ScTestexam scTestexam) {
        return  scTestexamMapper.insertScTestexam(scTestexam);
    }

    /**
     * 新增考试， 批量新增学生试卷
     * @param scTestexam
     * @return
     */
    @Override
    @Transactional
    public int insertScTestExamAndStudentTestExam(ScTestexam scTestexam) {
        int count = scTestexamMapper.insertScTestexam(scTestexam);

        List<Integer> studentIdList = scTestexam.getStudentIdList();

        if(studentIdList == null || studentIdList.size() <= 0)
            return count;

        return scStudentTestexamService.batchAddByStudentIds(scTestexam);
    }

    /**
     * 修改考试
     *
     * @param scTestexam 考试
     * @return 结果
     */
    @Override
    public int updateScTestexam(ScTestexam scTestexam) {
        return scTestexamMapper.updateScTestexam(scTestexam);
    }

    /**
     * 批量删除考试
     *
     * @param ids 需要删除的考试主键
     * @return 结果
     */
    @Override
    public int deleteScTestexamByIds(Long[] ids) {
        for (Long l : ids) {
            ScStudentTestexam scStudentTestexam = new ScStudentTestexam();
            scStudentTestexam.setTestexamid(l);
            //删除学生试卷
            scStudentTestexamMapper.deleteScStudentTestexam(scStudentTestexam);
        }
        return scTestexamMapper.deleteScTestexamByIds(ids);
    }

    /**
     * 删除考试信息
     *
     * @param id 考试主键
     * @return 结果
     */
    @Override
    public int deleteScTestexamById(Long id) {
        return scTestexamMapper.deleteScTestexamById(id);
    }
}
